/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cocos2d.common.sprite;

import cocos2d.common.CCPoint;
import cocos2d.common.CCRect;
import cocos2d.layer.CCLayer;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;
import tbgb.BaseGame;

/**
 *
 * @author KieuAnh
 */
public final class CCBaseSprite extends CCLayer implements CCSprite
{
    private final CCSpriteData data;
    private final CCRect currentFrame;
    private int currentFrameIndex;
    private boolean animate = true;
    private long delay = 0, cTime = 0;
    private int transform = Sprite.TRANS_NONE;
    
    public CCBaseSprite(CCSpriteData data)
    {
        super();
        
        this.data = data;
        this.currentFrame = new CCRect();
        this.currentFrameIndex = 0;
        this.delay = this.gameFPS();
    }
    
    public void draw(Graphics g, long dt)
    {
        if (this.isVisibled())
        {
            if (isAnimate() && getData().frames.length > 1)
            {
                this.cTime -= dt;
                if (this.cTime <= 0)
                {
                    this.setCurrentFrameIndex(this.getCurrentFrameIndex() + 1);
                }
            }
            
            if (!this.currentFrame.isEmpty())
            {
                CCPoint globalPos = this.getGlobalPosition();
                g.drawRegion(getData().image,
                        getCurrentFrame().getPosition().X.getInt(),
                        getCurrentFrame().getPosition().Y.getInt(),
                        getCurrentFrame().getSize().width().getInt(),
                        getCurrentFrame().getSize().height().getInt(), getTransform(),
                        globalPos.X.getInt(),
                        globalPos.Y.getInt(),
                        Graphics.TOP | Graphics.LEFT);
            }
            
            super.draw(g, dt);
        }
    }
    
    public float getFPS()
    {
        return (float) (1000 * this.getDelay()) / this.gameFPS();
    }
    
    public void setFPS(float fps)
    {
        this.delay = (long) ((fps * this.gameFPS()) / 1000f);
        this.cTime = this.getDelay();
    }
    
    private long gameFPS()
    {
        return BaseGame.DEFAULT_LOOP_INTERVAL;
    }

    /**
     * @return the data
     */
    public CCSpriteData getData() {
        return data;
    }

    /**
     * @return the currentFrame
     */
    public CCRect getCurrentFrame() {
        return currentFrame;
    }

    /**
     * @return the currentFrameIndex
     */

    public int getCurrentFrameIndex() {
        return currentFrameIndex;
    }

    /**
     * @param currentFrameIndex the currentFrameIndex to set
     */
    public void setCurrentFrameIndex(int currentFrameIndex) {
        this.currentFrameIndex = currentFrameIndex;
        
        if (this.getCurrentFrameIndex() >= this.getData().frames.length)
        {
            this.currentFrameIndex = 0;
        }

        this.getData().frameAtIndex(getCurrentFrameIndex(), getCurrentFrame());
        this.cTime = this.getDelay();
    }

    /**
     * @return the animate
     */
    public boolean isAnimate() {
        return animate;
    }

    /**
     * @param animate the animate to set
     */
    public void setAnimate(boolean animate) {
        this.animate = animate;
    }

    /**
     * @return the delay
     */
    public long getDelay() {
        return delay;
    }

    /**
     * @return the transform
     */
    public int getTransform() {
        return transform;
    }

    /**
     * @param transform the transform to set
     */
    public void setTransform(int transform) {
        this.transform = transform;
    }
}
